package org.xlp.auto.create;

import org.xlp.assertion.AssertUtils;
import org.xlp.auto.create.exception.AutoCreatorDataLoseException;
import org.xlp.auto.create.util.AutoCreatorUtils;
import org.xlp.utils.XLPStringUtil;

/**
 * Description: DaoImpl java文件自动创建类
 * <br/>date: 2023/11/30 11:05
 *
 * @version 1.0
 * @author: xlp
 */
public class DaoImplAutoCreator extends AbstractAutoCreator{
    private DaoAutoCreator daoAutoCreator;

    /**
     * 标记是否关联相应的Dao，默认不关联
     */
    private boolean relationDao = false;

    /**
     * 存储对应接口名称
     */
    private String daoName;

    public DaoImplAutoCreator(){
        setSubPackageName(Constants.DAO_SUB_PACKAGE_NAME + ".impl");
    }

    /**
     * 构造函数
     * @param entityName
     * @throws NullPointerException 假如参数为null，则抛出该异常
     */
    public DaoImplAutoCreator(String entityName) {
        this();
        setEntityName(entityName);
        String subPkgName = Constants.DAO_SUB_PACKAGE_NAME + "." + entityName.toLowerCase() + ".impl";
        setSubPackageName(subPkgName);
        this.relationDao = true;
    }

    /**
     * 构造函数
     * @param daoAutoCreator
     * @throws NullPointerException 假如参数为null，则抛出该异常
     */
    public DaoImplAutoCreator(DaoAutoCreator daoAutoCreator) {
        AssertUtils.isNotNull(daoAutoCreator, "daoAutoCreator parameter must not be null!");
        this.daoAutoCreator = daoAutoCreator;

        AutoCreatorUtils.autoCreatorConvert(this, daoAutoCreator);

        String subPkgName = getSubPackageName();
        subPkgName = XLPStringUtil.isEmpty(subPkgName) ? "impl" : subPkgName + ".impl";
        setSubPackageName(subPkgName);
        this.daoName = getDaoName();
    }

    /**
     * 构造函数
     *
     * @param abstractAutoCreator
     * @throws NullPointerException 假如参数为null，则抛出该异常
     */
    public DaoImplAutoCreator(AbstractAutoCreator abstractAutoCreator) {
        super(abstractAutoCreator);
        String subPkgName = Constants.DAO_SUB_PACKAGE_NAME + "." +
                abstractAutoCreator.getEntityName().toLowerCase() + ".impl";
        setSubPackageName(subPkgName);
        this.relationDao = true;
    }

    @Override
    public String[] getInterfaceNames() {
        String[] interfaceNames = super.getInterfaceNames();
        if ((daoAutoCreator == null || daoAutoCreator.getClassType() != ClassType.INTERFACE)
            && !relationDao) {
            return interfaceNames;
        }

        String interfaceName = XLPStringUtil.isEmpty(daoName) && relationDao ?
                getEntityName().toLowerCase() + "Dao" : daoName;
        this.addInterfaceName(interfaceName);

        return super.getInterfaceNames();
    }

    private String getDaoName(){
        if (daoAutoCreator != null) {
            try {
                return daoAutoCreator.getFileName().replace(Constants.JAVA_FILE_SUFFIX, "");
            } catch (AutoCreatorDataLoseException ignored) {
            }
        }
        return null;
    }

    @Override
    public String getSupperClassName() {
        String supperClassName = super.getSupperClassName();
        supperClassName = (XLPStringUtil.isEmpty(supperClassName) && daoAutoCreator != null
                && daoAutoCreator.getClassType() != ClassType.INTERFACE && !relationDao)
                ? daoName : supperClassName;
        return supperClassName;
    }

    /**
     * Java文件导入包内容
     *
     * @return
     */
    @Override
    protected String makeImportContent() {
        String importContent = "import org.xlp.bean.annotation.Component;\n";

        String packageName = StringUtils.getFullPackageName(daoAutoCreator);
        if (!XLPStringUtil.isEmpty(packageName)){
            importContent += "import " + packageName + "." + daoName + ";\n";
        } else if (relationDao){
            String basePackage = getRootPackageName();
            basePackage = XLPStringUtil.isEmpty(basePackage) ? XLPStringUtil.EMPTY : basePackage.trim() + ".";
            basePackage += Constants.DAO_SUB_PACKAGE_NAME + "." + getEntityName().toLowerCase() + "." + getEntityName() + "Dao";
            importContent += "import " + basePackage + ";\n";
        }
        // 导入包
        return importContent;
    }

    /**
     * java文件注解类容
     *
     * @return
     */
    @Override
    protected String makeAnnotationContent() {
        if (XLPStringUtil.isEmpty(daoName) && !relationDao){
            return "@Component\n";
        }
        if (!XLPStringUtil.isEmpty(daoName)) {
            return "@Component(id = \"" + StringUtils.firstCharToLowerCase(daoName) + "\")\n";
        }
        return "@Component(id = \"" + StringUtils.firstCharToLowerCase(getEntityName()) + "Dao\")\n";
    }

    /**
     * 获取java文件名称
     *
     * @return
     * @throws AutoCreatorDataLoseException 假如获取失败，则抛出该异常
     */
    @Override
    protected String getFileName() throws AutoCreatorDataLoseException {
        if (XLPStringUtil.isEmpty(getEntityName())){
            throw new AutoCreatorDataLoseException("实体名称未设置。");
        }
        return getEntityName() + "DaoImpl" + Constants.JAVA_FILE_SUFFIX;
    }
}
